package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.utils.JwtUtil;
import edu.ynu.se.xiecheng.achitectureclass.dto.UserLoginDTO;
import edu.ynu.se.xiecheng.achitectureclass.dto.UserRegisterDTO;
import edu.ynu.se.xiecheng.achitectureclass.entity.User;
import edu.ynu.se.xiecheng.achitectureclass.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 * @author xiecheng
 * @since 2024/01/01
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final JwtUtil jwtUtil;

    /**
     * 用户登录
     * @param loginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody UserLoginDTO loginDTO) {
        log.debug("收到登录请求: {}", loginDTO.getUsername());
        
        try {
            // 调用 service 层的登录方法
            String token = userService.login(loginDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("token", token);
            response.put("username", loginDTO.getUsername());
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                .body(Map.of("message", e.getMessage()));
        }
    }

    /**
     * 用户注册
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody UserRegisterDTO registerDTO) {
        log.debug("收到注册请求: {}", registerDTO.getUsername());
        
        try {
            // 调用 service 层的注册方法
            User user = userService.register(registerDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("message", "注册成功");
            response.put("username", user.getUsername());
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            log.error("注册失败: {}", e.getMessage());
            return ResponseEntity.badRequest()
                .body(Map.of("message", e.getMessage()));
        }
    }

    /**
     * 获取用户信息
     * @param token 用户令牌
     * @return 用户信息
     */
    @GetMapping("/info")
    public ResponseEntity<?> getUserInfo(@RequestHeader("Authorization") String token) {
        log.debug("获取用户信息，token: {}", token);
        
        if (!jwtUtil.verify(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                .body("无效的token");
        }
        
        String username = jwtUtil.getUsername(token);
        Map<String, Object> response = new HashMap<>();
        response.put("username", username);
        
        return ResponseEntity.ok(response);
    }
} 